package com.sfac.springboot.modules.test.dao;

import com.sfac.springboot.modules.test.entity.Student;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * StudentDao
 */
@Repository
public interface StudentDao extends JpaRepository<Student, Integer> {

	List<Student> findByStudentName(String studentName);

//	@Query(nativeQuery = true, value = "select * from test_student where if(?1==0, id=1, id=?1)")
//	@Query(nativeQuery = true, value = "select * from test_student where id = :id")
	@Query(value = "from Student where id = :id")
	Student getStudentById(@Param("id") int id);

	@Modifying
	@Query(nativeQuery = true,
			value = "update test_student set if(?2 is not null, student_name=?2, 1=1), " +
					"if(?3 is not null, update_date=?3, 1=1) where id = ?1")
	void updateStudent(@Param("id") int id,
					   @Param("studentName") String studentName,
					   @Param("studentName") LocalDateTime updateDate);
}
